﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RXY_Mall.Application.Statistics.Dto;
using System.Data.Entity;

namespace RXY_Mall.Application.Statistics
{
    public class StatisticsService : RXY_MallAppServiceBase, IStatisticsService
    {
        public AuditStatisticsDto GetAuditStatistics(int type)
        {
            var dto = new AuditStatisticsDto();
            switch (type)
            {
                case 1:
                    dto.user_withdrawal = db.gw_finance_withdraws.Count(i=>i.status==1);
                    break;
                case 2:
                    dto.shop_withdrawal = db.gw_shop_finance_withdrawals.Count(i => i.status == 1);
                    break;
                case 3:
                    dto.shop_bankcard_check = db.gw_shop_bankcards.Count(i => i.status == 0);
                    break;
                case 4:
                    //dto.shop_check = db.gw_finance_withdraws.Count(i => i.status == 1);
                    break;
                default:
                    dto.user_withdrawal = db.gw_finance_withdraws.Count(i => i.status == 1);
                    dto.shop_withdrawal = db.gw_shop_finance_withdrawals.Count(i => i.status == 1);
                    dto.shop_bankcard_check = db.gw_shop_bankcards.Count(i => i.status == 0);
                    break;
            }
            return dto;
        }

        public GoodsStatisticsDto GetGoodsStatistics(int shop = -1)
        {
            var today = DateTime.Now.Date;
            var goods=db.gw_goods
                .Include(u => u.gw_goods_property)
                .Include(u => u.gw_goods_sku)
                .Where(i => !i.deleted)
                .Select(item=> new {
                    item.id,item.
                    type_id, item.
                    add_time,
                    item.gw_goods_property.published,
                    sku_number=item.gw_goods_sku.Where(j => !j.deleted).Sum(j => j.stock_quantity),
                }).ToList();
            var dto = new GoodsStatisticsDto();
            dto.goods_total = goods.Count();
            dto.goods_today = goods.Count(i => i.add_time > today);
            dto.goods1_total = goods.Count(i => i.type_id == 1);
            dto.goods2_total = goods.Count(i => i.type_id == 2);
            dto.goods3_total = goods.Count(i => i.type_id == 3);
            dto.goods_publish = goods.Count(i => i.published==1&&i.sku_number>0);
            dto.goods_no_publish = goods.Count(i => i.published == 0);
            dto.goods_no_stock = goods.Count(i => i.published == 1 && i.sku_number == 0);
            return dto;
        }

        public OrderStatisticsDto GetHomeOrderStatistics(int shop = -1)
        {
            int[] statusArr = { 4, 5, 6, 10 };
            var today = DateTime.Now.Date;
            //var week = today.AddDays(1 - Convert.ToInt32(today.DayOfWeek)).Date;
            var month = today.AddDays(1 - Convert.ToInt32(today.Day)).Date;
            var year = today.AddMonths(1 - Convert.ToInt32(today.Month)).AddDays(1 - Convert.ToInt32(today.Day)).Date;
            var order = db.gw_orders
                .Include(o => o.gw_order_goods)
                .Where(i=> statusArr.Contains(i.status))
                .Select(item => new { item.id, item.status, item.add_time, item.gw_order_goods.FirstOrDefault().buy_type, item.gw_order_goods.FirstOrDefault().goods_type, item.goods_total }).ToList();
            var dto = new OrderStatisticsDto();
            dto.order_total = order.Count();
            dto.order_today = order.Count(i => i.add_time > today);

            dto.order_shipping = order.Count(i=>i.status==4);
            dto.order_shipping_money = order.Where(i => i.status == 4).Sum(i=>i.goods_total);

            dto.order_GOD_today = order.Count(i => i.goods_type == 1 && i.buy_type == 1 && i.add_time > today);
            dto.order_GOG_today = order.Count(i => i.goods_type == 1 && i.buy_type == 2 && i.add_time > today);
            dto.order_GMD_today = order.Count(i => i.goods_type == 3 && i.buy_type == 1 && i.add_time > today);
            dto.order_GFD_today = order.Count(i => i.goods_type == 2 && i.buy_type == 1 && i.add_time > today);

            dto.order_GOD_today_money  = order.Where(i => i.goods_type == 1 && i.buy_type == 1 && i.add_time > today).Sum(i => i.goods_total);
            dto.order_GOG_today_money  = order.Where(i => i.goods_type == 1 && i.buy_type == 2 && i.add_time > today).Sum(i => i.goods_total);
            dto.order_GMD_today_money  = order.Where(i => i.goods_type == 3 && i.buy_type == 1 && i.add_time > today).Sum(i => i.goods_total);
            dto.order_GFD_today_money  = order.Where(i => i.goods_type == 2 && i.buy_type == 1 && i.add_time > today).Sum(i => i.goods_total);

            dto.order_GOD_month = order.Count(i => i.goods_type == 1 && i.buy_type == 1 && i.add_time > month);
            dto.order_GOG_month = order.Count(i => i.goods_type == 1 && i.buy_type == 2 && i.add_time > month);
            dto.order_GMD_month = order.Count(i => i.goods_type == 3 && i.buy_type == 1 && i.add_time > month);
            dto.order_GFD_month = order.Count(i => i.goods_type == 2 && i.buy_type == 1 && i.add_time > month);

            dto.order_GOD_month_money = order.Where(i => i.goods_type == 1 && i.buy_type == 1 && i.add_time > month).Sum(i => i.goods_total);
            dto.order_GOG_month_money = order.Where(i => i.goods_type == 1 && i.buy_type == 2 && i.add_time > month).Sum(i => i.goods_total);
            dto.order_GMD_month_money = order.Where(i => i.goods_type == 3 && i.buy_type == 1 && i.add_time > month).Sum(i => i.goods_total);
            dto.order_GFD_month_money = order.Where(i => i.goods_type == 2 && i.buy_type == 1 && i.add_time > month).Sum(i => i.goods_total);

            dto.order_GOD_year = order.Count(i => i.goods_type == 1 && i.buy_type == 1 && i.add_time > year);
            dto.order_GOG_year = order.Count(i => i.goods_type == 1 && i.buy_type == 2 && i.add_time > year);
            dto.order_GMD_year = order.Count(i => i.goods_type == 3 && i.buy_type == 1 && i.add_time > year);
            dto.order_GFD_year = order.Count(i => i.goods_type == 2 && i.buy_type == 1 && i.add_time > year);

            dto.order_GOD_year_money = order.Where(i => i.goods_type == 1 && i.buy_type == 1 && i.add_time > year).Sum(i => i.goods_total);
            dto.order_GOG_year_money = order.Where(i => i.goods_type == 1 && i.buy_type == 2 && i.add_time > year).Sum(i => i.goods_total);
            dto.order_GMD_year_money = order.Where(i => i.goods_type == 3 && i.buy_type == 1 && i.add_time > year).Sum(i => i.goods_total);
            dto.order_GFD_year_money = order.Where(i => i.goods_type == 2 && i.buy_type == 1 && i.add_time > year).Sum(i => i.goods_total);

            return dto;
        }

        public UserStatisticsDto GetUserStatistics()
        {
            var today = DateTime.Now.Date;
            var user = db.gw_users.Where(i=>!i.deleted).Select(item => new { item.id, item.add_time }).ToList();
            var dto = new UserStatisticsDto();
            dto.user_total = user.Count();
            dto.user_today = user.Count(i=>i.add_time>today);
            return dto;
        }

        public UserStatisticsDto GetCompanyStatistics()
        {
            var today = DateTime.Now.Date;
            var user = db.gw_companys.Where(i => !i.deleted).Select(item => new { item.id, item.add_time }).ToList();
            var dto = new UserStatisticsDto();
            dto.user_total = user.Count();
            dto.user_today = user.Count(i => i.add_time > today);
            return dto;
        }

        public UserStatisticsDto GetShopStatistics()
        {
            var today = DateTime.Now.Date;
            var user = db.gw_shops.Where(i => !i.deleted).Select(item => new { item.id, item.add_time }).ToList();
            var dto = new UserStatisticsDto();
            dto.user_total = user.Count();
            dto.user_today = user.Count(i => i.add_time > today);
            return dto;
        }

        public VipStatisticsDto GetVipStatistics()
        {
            var today = DateTime.Now.Date;
            var vip = db.gw_order_member_payments.Select(item => new { item.id, item.add_time, item.pay_type }).ToList();
            var dto = new VipStatisticsDto();
            dto.vip_total = vip.Count();
            dto.vip_today = vip.Count(i => i.add_time > today);
            dto.vip_total_system = vip.Count(i => i.pay_type == 5);
            return dto;
        }

        public SettlementStatisticsDto GetSettlementStatistics(int shop = -1)
        {
            int[] arrStaus = { 1,2};
            var settlement = db.gw_shop_finance_settlements.Where(i => arrStaus.Contains(i.status)).Select(item=>new { item.id,item.money});
            var dto = new SettlementStatisticsDto();
            dto.settlement_number = settlement.Count();
            //dto.settlement_money = settlement.Sum(i=>i.money);
            return dto;
        }

        public UserStatisticsDto GetManagerStatistics()
        {
            var today = DateTime.Now.Date;
            var user = db.gw_managers.Where(i => !i.deleted).Select(item => new { item.id, item.add_time }).ToList();
            var dto = new UserStatisticsDto();
            dto.user_total = user.Count();
            dto.user_today = user.Count(i => i.add_time > today);
            return dto;
        }
    }
}
